<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page session="true" %>
<%@ page import="javax.portlet.*" %>
<%@page import ="rai.dto.*" %>
<%@page import ="rai.dao.*" %>
<%@page import = "java.util.*" %>

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<portlet:defineObjects/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Personal Information Tracking System</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<script type="text/javascript" src='<%= request.getContextPath() + "/js/calendarDateInput.js" %>'></script>
<script type="text/javascript">
    function getForm(namespace, name) {
        return document.forms[namespace + name];
    }
    function back_to_emp_info_form(namespace, name) {
        var frm = getForm(namespace, name);
        frm.txtAction.value = "back_to_empInfo_form_2";
        frm.submit();
    }
    function back_to_login(namespace, name) {
		var frm = getForm(namespace, name);
		frm.txtAction.value = "back_to_login_form";
		frm.submit();
	}
    function do_Add(namespace,name){
		var frm= getForm(namespace,name);
		frm.txtAction.value="do_add_project";
		frm.txtLanghid.value=getValuelst(frm.txtLang.name);
		frm.txtApphid.value=getValuelst(frm.txtApp.name);
		frm.txtTechhid.value=getValuelst(frm.txtTech.name);
		frm.txtFrwhid.value=getValuelst(frm.txtFrw.name);
		frm.txtDatahid.value=getValuelst(frm.txtDatabase.name);
		frm.submit();
	}
    function getValuelst(name){
		var lst=document.getElementsByName(name)[0];
		//alert(name+lst.options.length);
		var string="";
		for(i=0;i<lst.options.length;i++)
		{
			if(string==""){
				string = lst.options[i].value;
			}else{
				string = string+';'+lst.options[i].value;
			}
			//alert(string);
		}
		return string;
	}
	//Ajax Function
	function asynchGet(updateURL,lstresponse){
		 
	    if (window.XMLHttpRequest) {
	        portletReq = new XMLHttpRequest();
	    } else if (window.ActiveXObject) {
	        portletReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  // alert(updateURL+" "+ lstresponse);
	    portletReq.onreadystatechange = function() {processReqChange(lstresponse);};
	    
	    portletReq.open("GET", updateURL, true);
	    //alert(updateURL);
	    portletReq.send(null);
	}
	function processReqChange(lstresponse) {
		//alert(portletReq.status+" "+portletReq.readyState);
		//alert(lstresponse);
	    if (portletReq.readyState == 4) {
	        if (portletReq.status == 200) {
	            // process response
	            //alert("proccess");
	            displayDepartment(lstresponse);
	        }
	    }
	}
	function selectComp(evt,lstresponse) {
		
	    evt = (evt) ? evt : ((window.event) ? window.event : null);
	    if (evt) {
	    	
	        var select = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	        if (select && select.options.length > 1) {
	        	 //alert(lstresponse);
	           asynchGet('<%=request.getContextPath()%>'+"/SearchInfoServlet?lstComp=" + select.value+"&name="+lstresponse,lstresponse);
	        }
	    }
	}
		
	function displayDepartment(lstresponse) {
	    // substitute new invoice HTML content into "portletcontent" <div> tag
	    //alert(lstid);
	   // alert(lstresponse);
	    //alert("result"+portletReq.responseText);
	    var div = document.getElementById(lstresponse);
	    
	    div.innerHTML = "";
	   
	    
	    div.innerHTML = portletReq.responseText;
	}
	function init() {
		var div = document.getElementById("lstDept");
		div.innerHTML = "<select name=\"lstDept\" disabled > <option value=\"0\">All</option> </select>";
		
	}
	function removelist(lst){
		var elSel=document.getElementsByName(lst)[0];
		elSel.remove(elSel.selectedIndex);
		
	}
	
	function outputtolist(lst,txt){
		var lstFrw=document.getElementsByName(lst)[0];
		var txtFrw=document.getElementsByName(txt)[0];
		
		//alert(lst);
				if(txtFrw.options.length==0){
		  				value = lstFrw.value;
		  				
						txtFrw.options[0]= new Option(lstFrw.options[lstFrw.selectedIndex].text,value,true,false);
						
						
	  			}else
	  			{
	  				var temp = new Array();
	  				check= false;
	  				//alert('dzo');
	  				for(i=0;i<(txtFrw.options.length);i++)
	  				{
	  					
	  					temp= txtFrw.options[i].value.split("|");
	  					
	  					if(temp[0]==lstFrw.value)
	  					{
	  	  					txtFrw.options[i].text= lstFrw.options[lstFrw.selectedIndex].text;
	  						txtFrw.options[i].value= temp[0];
	  						check= true;
	  					}
	  				}
	  				if(!check){
		  				
		  				txtFrw.options[txtFrw.options.length]=new Option(lstFrw.options[lstFrw.selectedIndex].text,lstFrw.value,true,false);
		  				
		  				
		  			}
	  			}
	  		
		
	}
</script>
<link href='<%=request.getContextPath() + "/css/style.css" %>' rel="stylesheet" type="text/css" media="screen" />
</head>
<% ProjectEmpInfoDTO prjInfo = (ProjectEmpInfoDTO) session.getAttribute("prjInfo");
	%>
<body>
<form name="<portlet:namespace/>editPrj" action="<portlet:actionURL/>" method="POST">
<input type="hidden" name="txtAction"></input>
<input type="hidden" name="txtLanghid"></input>
<input type="hidden" name="txtApphid"></input>
<input type="hidden" name="txtFrwhid"></input>
<input type="hidden" name="txtTechhid"></input>
<input type="hidden" name="txtDatahid"></input>
<table>
	
	<tr class="title">
	
			<td colspan="2">		
					<h1><a href="#">Personal information tracking system </a></h1>
					<p>exploit the value of data</p>
					<hr />

			</td>
	</tr>
	<tr>
			<td colspan="2" align="left"> Welcome, <font color="orange"/><b>[${sessionScope.role}]${sessionScope.name}</b></font>
		| <a href="#" onclick="back_to_emp_info_form('<portlet:namespace/>','pmViewPrj')">Home</a>
		| <a href="#" onclick="back_to_login('<portlet:namespace/>','pmViewPrj')">Logout</a>
			
	   </td>
	</tr>
	<tr>	
		<td>
			<table border="1">
				<tr>
					<td><h2 class="title">Project  Information</h2></td>

				</tr>
					
				<tr>
					<td><p><em>General Information </em></p></td>
				</tr>
				<tr>
					<td>
						<table border="1" width="100%">							
							<tr>
							  <td align="left" valign="bottom">&nbsp;Project Name</td>

							  <td align="left" valign="middle" colspan="3"><input type="text" name="txtPrjName" value='<%=prjInfo.getSname() %>' /></td>							  							 						  </tr><tr>
							  <td align="left" valign="bottom">&nbsp;Project's Description&nbsp;</td>
							  <td align="left" valign="middle" colspan="3"><textarea name="txtPrjDes"><%=prjInfo.getDes() %></textarea></td>							  							</tr>
							<tr>
							  <td align="left" valign="bottom">&nbsp;Company&nbsp;</td>
							  <td align="left" valign="middle">
							  <select name="lstComp" onchange="selectComp(event,'lstDept')">
							<%SearchInfoDAO searchDAO = new SearchInfoDAO(); %>
							<% List<CompanyDTO> List_comp = new ArrayList<CompanyDTO>(); %>
							<% List_comp= searchDAO.getComp(); %>
							<% for (int i = 0; i < List_comp.size(); i++){
								CompanyDTO exp = new CompanyDTO();
								exp = List_comp.get(i);
								if(exp.getSname().equals(prjInfo.getCompany())) {
							%>
							  <option selected value=<%=exp.getId()%>><%=exp.getSname()%></option> 
							<%}else{ %>   
							  <option value=<%=exp.getId()%>><%=exp.getSname()%></option>
							<%}}; %>	
							</select>						  </td>							
							  <td colspan="2" align="left" valign="middle">Department
								<div id="lstDept"><%=prjInfo.getDept() %></div>
							  </td>  
						  </tr>
							<tr>

							  <td align="left" valign="middle" rowspan="2">&nbsp;Project's Period&nbsp;</td>
							  <td align="left" valign="bottom">From</td>
							  <td align="left" valign="middle" colspan="2">&nbsp;
							  <%String path = request.getContextPath(); %>
										<script>
											DateInput('periodFrom', true, 'DD/MM/YYYY',<%=path%>,<%=prjInfo.getStart()%>);
										</script>							  </td>  							 
						  </tr>
						  <tr>
							  	<td align="left" valign="bottom">&nbsp;To&nbsp;</td>

						  		<td align="left" valign="middle" colspan="2">&nbsp;
										<script>
											DateInput('periodTo', true, 'DD/MM/YYYY',<%=path%>,<%=prjInfo.getEnd()%>);
										</script>							  </td>
						  </tr>
						  <tr>
							  	<td align="left" valign="bottom">&nbsp;Size&nbsp;</td>
						  		<td colspan="3" align="left" valign="middle"><input type="text" name="txtPrjSize" value='<%=prjInfo.getSize() %>'/></td>
						  </tr>

						  <tr>
							  <td align="left" valign="bottom" colspan="4"><em>&nbsp;Technology Information&nbsp;</em></td>							  
						  </tr>
						  <tr>
							  <td align="left" valign="bottom">&nbsp;Language&nbsp;</td>
							  <td align="left" valign="bottom">
							  
							  	<select name="txtLang" size=3 style="width:180px" ondblclick="removelist(this.form.txtLang.name)">
							  	<%AddActionDAO addDAO = new AddActionDAO();
							  	  List<LangDTO> lst_prjlang = addDAO.getLang(prjInfo.getId(),1);
							  	  for(int i = 0; i < lst_prjlang.size();i++) {
							  	      LangDTO exp = lst_prjlang.get(i);
							  	%>	
							  		<option value ='<%=exp.getId()%>'><%=exp.getSname()%></option>
							  	<%} %>
							  	</select>
							  </td>
							  <td align="left" valign="bottom">		
							  	  				  
								  <select name="lstLang">				
														
									<%
									  List<LangDTO> list_lang= searchDAO.getLang();
									  for(int i=0;i<list_lang.size();i++){
									  LangDTO exp = new LangDTO();
									  exp=list_lang.get(i);
									  %>
									  	  <option value="<%=exp.getId()%>"><%=exp.getSname()%></option>
									  <%};%>
									  
                                    
                            </select> 
								</td>  
								<td align="left" valign="bottom">
								   <input name="addLang" type="button" value="Add" 
								  			onclick="outputtolist(this.form.lstLang.name,this.form.txtLang.name)"/>
								  	<input name="rmvLang" type="button" value="Remove" 
								  			onclick="removelist(this.form.txtLang.name)"/>	</td>
						  </tr>
						  <tr>
							  <td align="left" valign="bottom">&nbsp;Type of Application&nbsp;</td>
							  <td align="left" valign="bottom">
							  	<select name="txtApp" size=3 style="width:180px" ondblclick="removelist(this.form.txtApp.name)">
							  		<%
								  	  List<AppDTO> lst_applang = addDAO.getApp(prjInfo.getId(),1);
								  	  for(int i = 0; i < lst_applang.size();i++) {
								  	      AppDTO exp = lst_applang.get(i);
								  	%>	
								  		<option value ='<%=exp.getId()%>'><%=exp.getSname()%></option>
								  	<%} %>
							  	</select>
							  </td>
							  <td align="left" valign="bottom">	
							  <select name="lstApp">
							    <%List<AppDTO> list_app= searchDAO.getApp();
								  for(int i=0;i<list_app.size();i++){
									  AppDTO exp = new AppDTO();
									  exp=list_app.get(i);
								%>
									  	  <option value="<%=exp.getId()%>"><%=exp.getSname()%></option>
									  <% };%>
                                </select>
							  </td>
								<td align="left" valign="bottom">
						    <input name="addApp" type="button" value="Add" 
								  			onclick="outputtolist(this.form.lstApp.name,this.form.txtApp.name)"/>
							<input name="rmvApp" type="button" value="Remove" 
								  			onclick="removelist(this.form.txtApp.name)"/>
								</td>
						  </tr>
						  <tr>
							  <td align="left" valign="bottom">&nbsp;Kind of Technology&nbsp;</td>
							  <td align="left" valign="bottom">
							  	<select name="txtTech" size=3 style="width:180px" ondblclick="removelist(this.form.txtTech.name)">
							  		<%
								  	  List<TechDTO> lst_techlang = addDAO.getTech(prjInfo.getId(),1);
								  	  for(int i = 0; i < lst_techlang.size();i++) {
								  	      TechDTO exp = lst_techlang.get(i);
								  	%>	
								  		<option value ='<%=exp.getId()%>'><%=exp.getSname()%></option>
								  	<%} %>
							  	</select>
							  </td>
							  <td align="left" valign="bottom">
							  		<select name="lstTech">
							  		<%List<TechDTO> list_tech= searchDAO.getTech();
									  for(int i=0;i<list_tech.size();i++){
									  TechDTO exp = new TechDTO();
									  exp=list_tech.get(i);
									%>
									  	  <option value="<%=exp.getId()%>"><%=exp.getSname()%></option>
									  <% };%>
                                
                              		</select>
							</td>
								<td align="left" valign="bottom">
							<input name="addTech" type="button" value="Add" 
								  			onclick="outputtolist(this.form.lstTech.name,this.form.txtTech.name)"/>								
								  			<input name="rmvTech" type="button" value="Remove" 
								  			onclick="removelist(this.form.txtTech.name)"/>	</td>
						  </tr>
						  <tr>
							  <td align="left" valign="bottom">&nbsp;Kind of Framework&nbsp;</td>
							  <td align="left" valign="bottom">
							  	<select name="txtFrw" size="3" style="width: 180px" ondblclick="removelist(this.form.txtFrw.name)">
							  		<%
								  	  List<FrwDTO> lst_frwlang = addDAO.getFrw(prjInfo.getId(),1);
								  	  for(int i = 0; i < lst_frwlang.size();i++) {
								  	      FrwDTO exp = lst_frwlang.get(i);
								  	%>	
								  		<option value ='<%=exp.getId()%>'><%=exp.getSname()%></option>
								  	<%} %>
							  	</select>
							  </td>
							  <td align="left" valign="bottom">
							  	  <select name="lstFrw">
								     <%List<FrwDTO> list_frw= searchDAO.getFrw();
									  for(int i=0;i<list_frw.size();i++){
									  FrwDTO exp = new FrwDTO();
									  exp=list_frw.get(i);
									 %>
									  	  <option value="<%=exp.getId()%>"><%=exp.getSname()%></option>
									  <% };%>  
								  </select>
							  	  </td>
								<td align="left" valign="bottom">
							<input name="addFw" type="button" value="Add" 
								  			onclick="outputtolist(this.form.lstFrw.name,this.form.txtFrw.name)"/>
								  			<input name="rmvFrw" type="button" value="Remove" 
								  			onclick="removelist(this.form.txtFrw.name)"/>								
								  			</td>
						  </tr>
						  <tr>
							  <td align="left" valign="bottom">&nbsp;Kind of Database&nbsp;</td>
							  <td align="left" valign="bottom">
							  	<select name="txtDatabase" size="3" style="width: 180px" ondblclick="removelist(this.form.txtFrw.name)">
							  		<%
								  	  List<DatabaseDTO> lst_prjdata = addDAO.getDatabase(prjInfo.getId(),1);
								  	  for(int i = 0; i < lst_prjdata.size();i++) {
								  	      DatabaseDTO exp = lst_prjdata.get(i);
								  	%>	
								  		<option value ='<%=exp.getId()%>'><%=exp.getSname()%></option>
								  	<%} %>
							  	</select>
							  </td>
							  <td align="left" valign="bottom">
							  	  <select name="lstDatabase">
								     <%List<DatabaseDTO> list_data= searchDAO.getDatabase();
									  for(int i=1;i<list_data.size();i++){
									  DatabaseDTO exp = new DatabaseDTO();
									  exp=list_data.get(i);
									 %>
									  	  <option value="<%=exp.getId()%>"><%=exp.getSname()%></option>
									  <% };%>  
								  </select>
							  	  </td>
								<td align="left" valign="bottom">
							<input name="addFw" type="button" value="Add" 
								  			onclick="outputtolist(this.form.lstDatabase.name,this.form.txtDatabase.name)"/>
								  			<input name="rmvFrw" type="button" value="Remove" 
								  			onclick="removelist(this.form.txtDatabase.name)"/>								
								  			</td>
						  </tr>

						  <tr align="left" valign="bottom">
						  	  <td colspan="5"><input type="button" name="btnAdd" value="Add" onchange="do_Add('<portlet:namespace/>','addNewPrj')"/><input type="button" name="Button2" value="Cancel" /></td>
						  </tr>
				    </table>					</td>
				</tr>
			</table>			
		</td>

	</tr>

	<tr>
		<td colspan="2">
			<div style="clear: both;">&nbsp;</div>
			<div id="footer"><p>Development by HCMUT-Group3.</p>
			</div>
		</td>
	</tr>
</table>
</form>
</body>
</html>
